


#ifndef _SOCK_H
#define _SOCK_H

#include "public_def.h"

#define MAX_TCP_PACKET_LEN 2048
#define MAX_UDP_PACKET_LEN 2048
#define MAX_SOCKET_NUM 256

#define	SOCK_SUCC 0x0000
#define SOCK_ERR_OPEN 0x1001
#define SOCK_ERR_CLOSE 0x1002
#define SOCK_ERR_BIND 0x1003
#define SOCK_ERR_SOCKET 0x1004
#define SOCK_ERR_LISTEN 0x1005
#define SOCK_ERR_GETSOCKNAME 0x1006


typedef int (*OnUDPSocketRecvCallBack)(void * buf, int len, struct sockaddr_in *addr, int port, void *param);

typedef struct
{
	struct sockaddr_in addr;
	int sock;
	int len;
	int sid;
    int m_blExitFlg;
    int m_iOpen;
	void *obj;
} ClientSock_t;

typedef struct
{
	int fd;
	struct sockaddr_in addr;
	int len;
	int family;
	int type;
	int port;
	fd_set fdset;
	pthread_t threadAccept, threadRead;
} ServerSock_t;

typedef struct
{
	void * param;
	ServerSock_t sock;
	OnUDPSocketRecvCallBack callback;
}UPDThreadParam_t;

typedef struct
{
	int socknum;
	OnUDPSocketRecvCallBack callback;
}UDPSelectParam_t;

typedef int (*OnSocketAcceptCallBack)(ClientSock_t * sock, void * param);
typedef int (*OnTCPSocketRecvCallBack)(void * buf, int len, ClientSock_t *sock, int port, void *param);
typedef void (*OnSocketDisconnectCallBack)(ClientSock_t * sock, void * param);

typedef struct
{
	void * param;
	ServerSock_t sock;
	ClientSock_t clientSock;
	OnSocketAcceptCallBack acceptCallback;
	OnTCPSocketRecvCallBack recvCallback;
	OnSocketDisconnectCallBack disconnectCallback;
}TCPThreadParam_t;


//Global variable definations


//Close UDP server socket.
//
//Parameter:
//	sock			Server socket.
//Return:
//	0 succ.
//
 int CloseUDPSvrSocket(ServerSock_t *sock);

//Open UDP server socket.
//
//Parameter:
//	sock			Server socket.
//	port			Server port.
//	callback	Callback handle for data receiver.
//	param			Input param.
//
//Return:
//	0 succ.
//
 int OpenUDPSvrSocket(ServerSock_t *sock, int port, OnUDPSocketRecvCallBack callback, void *param);

//Open UDP client socket
//
//Parameter:
//	sock			Client socket.	
//	addr			Remote IP address
//	port 			Remote port
//
//Return:
//	0 succ.
//
 int OpenUDPClientSocket(ClientSock_t * sock, char *addr, int port);

//Send data via UDP socket
//
//Parameter:
//	sock			Client socket.
//	buf				Input buffer
//	buflen		Input buffer length.
//
//Return:
//	bytes send.
//
 int SendUDPClientData(ClientSock_t * sock, void * buf, int buflen);

//Close UDP client socket
//
//Parameter:
//	sock			Client socket.
//
//Return:
//	0 succ.
//
 int CloseUDPClientSocket(ClientSock_t * sock);

//Send data via TCP socket
//
//Parameter:
//	sock			TCP socket.
//	buf				Input buffer.
//	len				Input buffer length.
//
//Return:
//	bytes send.
//
 int TCPSocketSend(ClientSock_t *sock, char * _cBuffer, int _iLength);

//Open TCP server socket.
//
//Parameter:
//	sock								Server socket.
//	port 								Server port.
//	param								Reference of input parameter.
//	acceptCallback			Callback handle for connection acception.
//	recvCallback				Callback handle for receiving data
//	disconnectCallback	Callback handle for socket disconnection.
//	
//Return:
//	0 succ.
//
 int OpenTCPSvrSocket(ServerSock_t *sock, int port, void * param, OnSocketAcceptCallBack acceptCallback, 
	OnTCPSocketRecvCallBack recvCallback, OnSocketDisconnectCallBack disconnectCallback);

//Close TCP server connection
//
//Parameter:
//	sock				Server ket	
//
//Return:
//	0 succ.
//
 int CloseTCPSvrSocket(ServerSock_t *sock);

//Close TCP Client connection
//
//Parameter:
//	sock				Client socket
//	
//Return:
//	0 succ.
//
 int CloseTCPClientSocket(ClientSock_t *sock);

//Get IP address
//
//Parameter:
//	sock				Socket connection.
//	buf					Output buffer.
//
//Return:
//	None.
//
 void GetIP(ClientSock_t *sock, char *buf);

 int GetPort(ClientSock_t * sock, unsigned short * _pport);

int TCPSocketSendNoWait(ClientSock_t *sock, char * buf, int len);

 int OpenUDPClientSocketNew(ClientSock_t * _csock, ServerSock_t*_ssock, char *addr, int port);

 int CreateUDPSelectThread(int socknum, OnUDPSocketRecvCallBack callback);
 void ReleaseUdpSelectThread();

 int AddUDPSelectSocket(ServerSock_t *sock, int _chn, int isVideo);
#endif



